package com.icss.ah.productPublish.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.SyncSelectTree;
import com.icss.ah.base.util.Escape;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.productPublish.service.ProductPublishService;

@Controller
@Scope("prototype")
public class ProductPublishAction extends BaseAction {

	private static final long serialVersionUID = 6738583796067559930L;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");

	@Autowired
	private ProductPublishService productPublishService;

	private String level;
	private String itemCode;
	private String itemName;
	private String viewCode;
	private String codeName;
	private String viewFlag;
	private String flag;
	private String productId;
	private String proName;
	private String brandName;
	private String areaCode;// 销售区域
	private String proStatus;
	private String typePCode;

	public String toProductPublishOnePage() {
		return "toProductPublishOnePage";
	}

	/**
	 * 页面选择类目列表查询
	 */
	public void getCategoryList() {
		JSONObject json = new JSONObject();
		Map<String, String> map = new HashMap<String, String>();
		map.put("level", level);
		map.put("itemCode", itemCode);
		map.put("itemName", itemName);
		System.out.println(productPublishService + "===" + map);
		List<Map<String, Object>> list = productPublishService.getCategoryList(map);
		json.put("list", list);
		str2Resp(json.toString());
	}

	/**
	 * 页面搜索列表查询
	 */
	public void queryCategoryList() {
		JSONObject json = new JSONObject();
		Map<String, String> map = new HashMap<String, String>();
		map.put("itemName", itemName);
		List<Map<String, String>> list = productPublishService.getCategoryNameList(map);
		json.put("list", list);
		str2Resp(json.toString());
	}

	public String toProductPublishTwoPage() {
		String areaName = "";
		try {
			itemName = new String(itemName.getBytes("ISO-8859-1"), "UTF-8");
			if (itemName != null && itemName.indexOf("%") >= 0) {
				itemName = Escape.unescape(Escape.unescape(itemName));
			}
			String userCode = getUserCodeFromSession();
			UserInfoBean userInfo = getUserInfo(userCode);
			String corpId = userInfo.getCorpId();
			String shopId = userInfo.getShopId();
			Map<String, String> map = new HashMap<String, String>();
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			map.put("productId", "0");
			List<Map<String, Object>> areaList = productPublishService.queryShopAreaList(map);
			if (areaList.size() > 0) {
				for (Map<String, Object> aMap : areaList) {
					areaCode += (String) aMap.get("AREA_CODE").toString() + ",";
					areaName += (String) aMap.get("ANAME").toString() + ",";
				}
				if (!"".equals(areaCode)) {
					areaCode = areaCode.substring(0, areaCode.length() - 1);
					areaName = areaName.substring(0, areaName.length() - 1);
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			areaName = "请先设定店铺销售区域";
		}
		itemName = itemName.replace(",", ">>");
		request = getRequest();
		request.setAttribute("itemName", itemName);
		request.setAttribute("proName", itemName.replace(">>", ""));
		request.setAttribute("itemCode", itemCode);
		request.setAttribute("areaCode", areaCode);
		request.setAttribute("areaName", areaName);
		return "toProductPublishTwoPage";
	}

	/**
	 * 商品参数查询
	 */
	public void queryBaseParam() {
		JSONObject json = new JSONObject();
		Map<String, String> map = new HashMap<String, String>();
		map.put("itemCode", itemCode);
		List<Map<String, Object>> list = productPublishService.getBaseParam(map);
		json.put("baseParaList", list);
		str2Resp(json.toString());
	}

	/**
	 * 下拉列表字典查询
	 */
	public void queryDirectoryInfo() {
		JSONObject json = new JSONObject();
		Map<String, String> map = new HashMap<String, String>();
		map.put("viewCode", viewCode);
		map.put("codeName", codeName);
		map.put("viewFlag", viewFlag);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if ("1".equals(flag))
			list = productPublishService.getDirectoryInfo(map);
		else
			list = productPublishService.getMeasuringInfo(); // 计量单位
		json.put("directoryList", list);
		str2Resp(json.toString());
	}

	/**
	 * 计价单位
	 */
	public void queryUnitInfo() {
		JSONObject json = new JSONObject();
		Map<String, String> map = new HashMap<String, String>();
		if (StringUtils.isNotEmpty(itemCode)) {
			String pCode = itemCode.substring(0, 3); // 获取父类
			//针对其他处理信息 特殊处理
			if("009999".equals(itemCode)) {
				pCode = "009999";
			}
			map.put("unitBelong", pCode);
		}

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		list = productPublishService.queryUnitInfo(map); // 计量单位
		json.put("directoryList", list);
		str2Resp(json.toString());
	}

	public void getTree() {
		request = getRequest();
		// id 异步加载展开树时传到后台的id
		// tarEnt 入口编码，以编码为入口编码的节点作为根节点
		// ifAll 是否一次性展示所有数据，1是 0否
		// String id = request.getParameter("id");
		// id = id == null ? "" : id;
		// String ifAll = request.getParameter("ifAll");
		// ifAll = ifAll == null ? "1" : ifAll;
		// SyncSelectTree selectTree = new SyncSelectTree();
		// selectTree.setId(id);
		// selectTree.setIfAll(ifAll);
		Map<String, String> map = new HashMap<String, String>();
		String userCode = getUserCodeFromSession();
		UserInfoBean userInfo = getUserInfo(userCode);
		String corpId = userInfo.getCorpId();
		String shopId = userInfo.getShopId();
		map.put("corpId", corpId);
		map.put("shopId", shopId);
		List<SyncSelectTree> list = productPublishService.getTree(map);
		System.out.println(list.size() + "========" + map);
		List<Map<String, Object>> checkList = productPublishService.queryShopAreaList(map);
		checkList = checkList == null ? new ArrayList<Map<String, Object>>() : checkList;
		for (SyncSelectTree sst : list) {
			sst.setOpen(true);
			if (sst.getIsParent() && "0".equals(sst.getpId())) {
				sst.setNocheck(true);
			}
			String sstAreaCode = sst.getId();
			if (!checkList.isEmpty()) {
				for (Map<String, Object> m : checkList) {
					String mAreaCode = (String) m.get("AREA_CODE") == null ? "" : m.get("AREA_CODE").toString();
					if (sstAreaCode.equals(mAreaCode)) {
						sst.setChecked(true);
					}
				}
			}
		}
		str2Resp(JSONArray.fromObject(list).toString());
	}

	public void getBrandInfo() {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("brandName", brandName);
		List<Map<String, Object>> list = productPublishService.queryBrandInfoList(map);
		str2Resp(JSONArray.fromObject(list).toString());
	}

	private String productName;
	private String unit;

	public String toUnitInfoPage() {
		try {
			productName = new String(productName.getBytes("ISO-8859-1"), "UTF-8");
			if (productName != null && productName.indexOf("%") >= 0) {
				productName = Escape.unescape(Escape.unescape(productName));
			}
			unit = new String(unit.getBytes("ISO-8859-1"), "UTF-8");
			if (unit != null && unit.indexOf("%") >= 0) {
				unit = Escape.unescape(Escape.unescape(unit));
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		request = getRequest();
		request.setAttribute("productId", productId);
		request.setAttribute("productName", productName);
		request.setAttribute("unit", unit);
		return "toUnitInfoPage";
	}

	public void getUnitInfo() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("productId", productId);
		map.put("productName", productName);
		map.put("unit", unit);
		map.put("type", "1");
		JSONArray arr = productPublishService.getUnitInfo(map);
		str2Resp(arr.toString());
	}

	private String unitName;
	private String buyPrice;
	private String scale;
	private String retailPrice;

	public void setUnitInfo() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("productId", productId);
		map.put("productName", productName);
		map.put("scale", scale);
		map.put("unit", unit);
		map.put("unitName", unitName);
		map.put("buyPrice", buyPrice);
		map.put("retailPrice", retailPrice);
		String result = productPublishService.setUnitInfo(map);
		str2Resp(result);
	}

	public void queryUnitList() {
		List<Map<String, Object>> list = productPublishService.queryUnitList();
		str2Resp(JSONArray.fromObject(list).toString());
	}

	/**
	 * 商品基本信息保存
	 * 
	 * @date 2017-7-25
	 *
	 */
	public void saveTwo() {
		JSONObject json = new JSONObject();
		try {
			request = getRequest();
			Map<String, String> map = new HashMap<String, String>();
			productId = productId == null || "".equals(productId) ? UUID.randomUUID().toString() : productId;// 商品编码
			String userCode = getUserCodeFromSession();
			UserInfoBean userInfo = getUserInfo(userCode);
			String corpId = userInfo.getCorpId();
			String shopId = userInfo.getShopId();
			String proName = request.getParameter("proName");// 商品名称
			String brandCode = request.getParameter("brandCode");// 品牌名称
			String categoryCode = request.getParameter("itemCode");// 商品类目
			String buyerRange = request.getParameter("buyerRange");// 买家范围
			String barCode = request.getParameter("barCode");// 条形码
			// String unitCode = request.getParameter("unitCode");//计价单位
			// String unitName = request.getParameter("unitName");//计价单位
			String measuringNew = request.getParameter("measuringNew");// 计价单位
			measuringNew = measuringNew == null ? "" : measuringNew;
			String proUnitCode = "";
			String proUnitName = "";
			if (measuringNew.length() > 0 && measuringNew.split("#@").length > 1) {
				proUnitCode = measuringNew.split("#@")[0];
				proUnitName = measuringNew.split("#@")[1];
			}
			buyerRange = null != buyerRange && "全部".equals(buyerRange) ? "1" : "2";
			map.put("userCode", userCode);
			map.put("productId", productId);
			map.put("barCode", barCode);
			map.put("brandCode", brandCode);
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			map.put("productName", proName);
			map.put("categoryCode", categoryCode);
			map.put("productUnit", proUnitName);
			map.put("proUnitCode", proUnitCode);
			map.put("proUnitName", proUnitName);
			map.put("status", "0");
			map.put("proStatus", "1");
			map.put("buyerRange", buyerRange);

			List<Map<String, String>> listPara = new ArrayList<Map<String, String>>();
			String colNames = request.getParameter("colNames");// 动态商品参数
			String[] colArr = colNames.split(",");
			String[] paraArr = request.getParameter("paraNames").split(",");
			String[] showArr = request.getParameter("paramShows").split(",");
			for (int i = 1; i < colArr.length; i++) {
				String para = colArr[i];
				Map<String, String> m = new HashMap<String, String>();
				String paraUuid = UUID.randomUUID().toString();
				m.put("paraUuid", paraUuid);
				m.put("corpId", corpId);
				m.put("shopId", shopId);
				m.put("productId", productId);
				m.put("paraName", paraArr[i]);
				String paraVal = request.getParameter(para);
				m.put("paraVal", paraVal);
				String paramShow = i > showArr.length - 1 ? "0" : showArr[i];
				m.put("paramShow", paramShow);
				para = para.replace("att", "");
				String pUuid = request.getParameter("pUuid" + para);
				m.put("pUuid", pUuid);
				if ("-1".equals(pUuid))
					m.put("setType", "2");
				else
					m.put("setType", "1");
				m.put("status", "1");
				m.put("sortNum", i + "");
				listPara.add(m);
			}

			List<Map<String, String>> listKey = new ArrayList<Map<String, String>>();
			for (int i = 1; i <= 5; i++) {
				Map<String, String> m = new HashMap<String, String>();
				String key = "keyWord" + i;
				String proKey = request.getParameter(key);
				m.put("proKey", proKey);// 关键词内容
				String keyUuid = UUID.randomUUID().toString();
				m.put("keyUuid", keyUuid);
				m.put("corpId", corpId);
				m.put("shopId", shopId);
				m.put("productId", productId);
				m.put("status", "1");
				m.put("sortNum", i + "");
				listKey.add(m);
			}

			// 商品销售区域
			List<Map<String, String>> areaList = new ArrayList<Map<String, String>>();
			if (null != areaCode && !"".equals(areaCode)) {
				areaCode = areaCode.replace("null", "");
				String[] areaArr = areaCode.split(",");
				for (int i = 0; i < areaArr.length; i++) {
					Map<String, String> obj = new HashMap<String, String>();
					obj.put("corpId", corpId);
					obj.put("shopId", shopId);
					obj.put("productId", productId);
					obj.put("areaCode", areaArr[i]);
					obj.put("uuid", UUID.randomUUID().toString());
					areaList.add(obj);
				}
			}

			boolean result = productPublishService.saveTwo(map, listPara, listKey, areaList);

			if (result) {
				json.put("saveFlag", "1");
			} else {
				json.put("saveFlag", "0");
			}
			json.put("productId", productId);
		} catch (Exception e) {
			json.put("saveFlag", "0");
			e.printStackTrace();
			errlog.error("ProductPublishAction|saveTwo：", e);
		} finally {
			str2Resp(json.toString());
		}
	}

	/**
	 * 商品图片和图文信息
	 * 
	 * @date 2017-7-25
	 *
	 */
	public String toProductPublishThrPage() {
		try {
			itemName = new String(itemName.getBytes("ISO-8859-1"), "UTF-8");
			if (itemName != null && itemName.indexOf("%") >= 0) {
				itemName = Escape.unescape(Escape.unescape(itemName));
			}
			proName = new String(proName.getBytes("ISO-8859-1"), "UTF-8");
			if (proName != null && proName.indexOf("%") >= 0) {
				proName = Escape.unescape(Escape.unescape(proName));
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		request = getRequest();
		request.setAttribute("itemName", itemName);
		request.setAttribute("productId", productId);
		request.setAttribute("proName", proName);
		return "toProductPublishThrPage";
	}

	public void saveThr() {
		JSONObject json = new JSONObject();
		try {
			String userCode = getUserCodeFromSession();
			UserInfoBean userInfo = getUserInfo(userCode);
			String corpId = userInfo.getCorpId();
			String shopId = userInfo.getShopId();

			request = getRequest();
			String postDesc = request.getParameter("postDesc");// 商品描述

			Map<String, String> map = new HashMap<String, String>();
			map.put("productId", productId);
			map.put("proStatus", "2");
			postDesc = postDesc == null || "".equals(postDesc) ? "本公司供应，质量保证，欢迎咨询洽谈。" : postDesc;
			map.put("productDesc", postDesc);

			List<Map<String, String>> listImg = new ArrayList<Map<String, String>>();
			String imgNum = request.getParameter("imgNum");
			infolog.info("ProductPublishAction|saveThr|imgNum:" + imgNum);
			if (null != imgNum && !"".equals(imgNum)) {
				int[] arr = getIntArr(imgNum);
				for (int i = 0; i < arr.length; i++) {
					Map<String, String> m = new HashMap<String, String>();
					String uuid = UUID.randomUUID().toString();
					m.put("uuid", uuid);
					m.put("corpId", corpId);
					m.put("shopId", shopId);
					m.put("productId", productId);
					m.put("status", "1");
					String imgUrl = request.getParameter("pic" + arr[i]);
					m.put("proPic", imgUrl);
					m.put("sortNum", (i + 1) + "");
					infolog.info("ProductPublishAction|saveThr|image:" + m);
					listImg.add(m);
				}
			}

			boolean result = productPublishService.saveThr(map, listImg);

			if (result) {
				json.put("saveFlag", "1");
			} else {
				json.put("saveFlag", "0");
			}
			json.put("productId", productId);
		} catch (Exception e) {
			json.put("saveFlag", "0");
			e.printStackTrace();
			errlog.error("ProductPublishAction|saveThr：", e);
		} finally {
			str2Resp(json.toString());
		}
	}

	public int[] getIntArr(String args) {
		String[] arr = args.split(",");
		int[] intArr = new int[arr.length - 1];
		for (int i = 1; i < arr.length; i++) {
			intArr[i - 1] = Integer.valueOf(arr[i]);
		}
		Arrays.sort(intArr);
		return intArr;
	}

	public String toProductPublishFourPage() {
		try {
			itemName = new String(itemName.getBytes("ISO-8859-1"), "UTF-8");
			if (itemName != null && itemName.indexOf("%") >= 0) {
				itemName = Escape.unescape(Escape.unescape(itemName));
			}
			proName = new String(proName.getBytes("ISO-8859-1"), "UTF-8");
			if (proName != null && proName.indexOf("%") >= 0) {
				proName = Escape.unescape(Escape.unescape(proName));
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		UserInfoBean userInfo = getUserInfo("");
		String corpId = userInfo.getCorpId();
		String shopId = userInfo.getShopId();
		Map<String, String> map = new HashMap<String, String>();
		map.put("corpId", corpId);
		map.put("shopId", shopId);
		List<Map<String, Object>> list = productPublishService.queryTemplateInfo(map);
		map.put("productId", productId);
		Map<String, Object> proInfo = productPublishService.getProInfo(map);
		// 添加订购类型
		Integer proType = 4;
		if (proInfo.containsKey("proType")) {
			proType = (Integer) proInfo.get("proType");
		}
		proInfo.put("proTypeName", coverTypeToName(proType));
		request = getRequest();
		request.setAttribute("itemName", itemName);
		request.setAttribute("productId", productId);
		request.setAttribute("proName", proName);
		request.setAttribute("list", list);
		request.setAttribute("pro", proInfo);
		return "toProductPublishFourPage";
	}

	private String coverTypeToName(Integer proType) {
		String proTypeName = "月订";
		switch (proType) {
		case 3:
			proTypeName = "季订";
			break;
		case 2:
			proTypeName = "半年";
			break;
		case 1:
			proTypeName = "年度";
			break;
		default:
			proTypeName = "月订";
			break;
		}

		return proTypeName;

	}

	public void saveFour() {
		JSONObject json = new JSONObject();
		try {
			request = getRequest();
			Map<String, String> map = new HashMap<String, String>();
			String userCode = getUserCodeFromSession();
			UserInfoBean userInfo = getUserInfo(userCode);
			String corpId = userInfo.getCorpId();
			String shopId = userInfo.getShopId();
			String shopTypeCode = request.getParameter("shopTypeCode");// 店内商品分类
//			String measuring = request.getParameter("measuring");// 计价单位
//			String measuringNew = request.getParameter("measuringNew");// 计价单位
//			measuringNew = measuringNew == null ? "" : measuringNew;
//			String proUnitCode = "";
//			String proUnitName = "";
//			if (measuringNew.length() > 0 && measuringNew.split("#@").length > 1) {
//				proUnitCode = measuringNew.split("#@")[0];
//				proUnitName = measuringNew.split("#@")[1];
//			}

			String unitWeight = request.getParameter("unitWeight");// 单位重量
			String delDays = request.getParameter("sendDate");// 发货日期
			String allowReturn = request.getParameter("allowReturn");// 是否允许退换货
			String productName = request.getParameter("proName");// 商品名称
			String proType = request.getParameter("proType");// 商品订购类型
																// 1-年度2-半年3-季订4-月订
			allowReturn = null != allowReturn && "是".equals(allowReturn) ? "1" : "0";
			String reg = "[\u4e00-\u9fa5]";
			delDays = Pattern.compile(reg).matcher(delDays).replaceAll("");
			String proStock = request.getParameter("proStock");// 商品库存
			proStock = proStock == null || "".equals(proStock) ? "0" : proStock;
			String proPrice = request.getParameter("proPrice");// 成本价格
			proPrice = proPrice == null || "".equals(proPrice) ? "0" : proPrice;
			String price = request.getParameter("price");
			price = price == null || "".equals(price) ? "0" : price;
			String priceTotal = request.getParameter("priceTotal");
			priceTotal = priceTotal == null || "".equals(priceTotal) ? "0" : priceTotal;
			String templateId = request.getParameter("templateId");
			map.put("productId", productId);
			map.put("allowReturn", allowReturn);
			map.put("typeCode", shopTypeCode);
			map.put("userCode", userCode);
			// map.put("productUnit", measuring);
			// map.put("proUnitCode", proUnitCode);
			// map.put("proUnitName", proUnitName);
			unitWeight = unitWeight == null || "".equals(unitWeight) ? "0" : unitWeight;
			map.put("unitWeight", unitWeight);
			map.put("delDays", delDays);
			map.put("proStock", proStock);
			map.put("proPrice", proPrice);
			map.put("proRetailPrice", price);
			map.put("proRetailPriceTotal", priceTotal);
			map.put("proStatus", "3");
			map.put("status", "1");
			map.put("productName", productName);
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			map.put("templateId", templateId);
			map.put("proType", proType);

			Map<String, String> m = new HashMap<String, String>();
			String uuid = UUID.randomUUID().toString();
			m.put("uuid", uuid);
			m.put("corpId", corpId);
			m.put("shopId", shopId);
			m.put("productId", productId);
			m.put("status", "1");
			m.put("purchaseLow", "1");
			m.put("type", "1");
			m.put("purchaseUp", "-1");
			m.put("price", price);
			m.put("sortNum", "1");

			List<Map<String, String>> listBuy = new ArrayList<Map<String, String>>();
			listBuy.add(m);

			// for(int i=0;i<3;){
			// String buyNum = request.getParameter("buyNum"+i);
			// if(buyNum == null || "".equals(buyNum)) break;
			// Map<String, String> m = new HashMap<String, String>();
			// String uuid = UUID.randomUUID().toString();
			// m.put("uuid", uuid);
			// m.put("corpId", corpId);
			// m.put("shopId", shopId);
			// m.put("productId", productId);
			// m.put("status", "1");
			// m.put("purchaseLow", buyNum);
			// String buyNum1 = "-1";//
			// String type = "1";
			// if((i+1)<3){
			// buyNum1 = request.getParameter("buyNum"+(i+1));
			// if(buyNum1 != null && !"".equals(buyNum1)){
			// buyNum1 = Integer.valueOf(buyNum1)-1+"";
			// type = "2";
			// } else {
			// buyNum1 = "-1";
			// }
			// }
			// m.put("type", type);
			// m.put("purchaseUp", buyNum1);
			// String price = request.getParameter("price"+i);
			// m.put("price", price);
			// m.put("sortNum", ++i+"");
			// listBuy.add(m);
			// }

			boolean result = productPublishService.saveFour(map, listBuy);

			if (result) {
				json.put("saveFlag", "1");
			} else {
				json.put("saveFlag", "0");
			}
			json.put("productId", productId);
		} catch (Exception e) {
			json.put("saveFlag", "0");
			e.printStackTrace();
			errlog.error("ProductPublishAction|saveFour：", e);
		} finally {
			str2Resp(json.toString());
		}
	}

	/**
	 * 店铺类别查询
	 */
	public void getShopType() {
		JSONObject json = new JSONObject();
		try {
			String typeFlag = getRequest().getParameter("typeFlag");
			Map<String, String> map = new HashMap<String, String>();
			String userCode = getUserCodeFromSession();
			map.put("userCode", userCode);
			String corpId = getBaseCorpId(); // 从配置文件里取公众号id
			map.put("corpId", corpId);
			map.put("level", level);
			map.put("typePCode", typePCode);
			map.put("typeFlag", typeFlag);
			List<Map<String, Object>> list = productPublishService.getShopType(map);
			json.put("shopTypeList", list);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeAction|getShopType：", e);
		} finally {
			str2Resp(json.toString());
		}

	}

	public String toProductPublishFivePage() {
		getRequest().setAttribute("productId", productId);// 商品编码传入页面
		return "toProductPublishFivePage";
	}

	public String toProductPublishInfoPage() {
		request = getRequest();
		String userCode = getUserCodeFromSession();
		UserInfoBean userInfo = getUserInfo(userCode);
		String corpId = userInfo.getCorpId();
		String shopId = userInfo.getShopId();
		String auditStatus = request.getParameter("auditStatus"); // 审核商品状态
		auditStatus = auditStatus == null ? "" : auditStatus;
		String uuid = request.getParameter("uuid"); // 审核商品id
		uuid = uuid == null ? "" : uuid;
		String infoFlag = request.getParameter("pageFlag");
		infoFlag = infoFlag == null ? "" : infoFlag;
		request.setAttribute("infoFlag", infoFlag);
		if (infoFlag.equals("copyProduct")) {
			String newProductId = UUID.randomUUID().toString();
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			map.put("newProductId", newProductId);
			map.put("productId", productId);
			boolean result = productPublishService.copyProduct(map);
			if (result) {
				productId = newProductId;
			}
			request.setAttribute("copyResult", result);
		}
		Map<String, Object> auditInfo = productPublishService.getAuditInfo(uuid);
		Map<String, String> map = new HashMap<String, String>();
		map.put("productId", productId);
		map.put("corpId", corpId);
		map.put("shopId", shopId);
		Map<String, Object> proInfo = productPublishService.getProInfo(map);
		List<Map<String, Object>> areaList = productPublishService.queryShopAreaList(map);
		String areaName = "";
		if (areaList.size() > 0) {
			for (Map<String, Object> aMap : areaList) {
				areaCode += (String) aMap.get("AREA_CODE").toString() + ",";
				areaName += (String) aMap.get("ANAME").toString() + ",";
			}
			if (!"".equals(areaCode)) {
				areaCode = areaCode.substring(0, areaCode.length() - 1);
				areaName = areaName.substring(0, areaName.length() - 1);
			}
		}
		String allowReturn = proInfo.get("allowReturn") == null ? "" : proInfo.get("allowReturn").toString();
		allowReturn = allowReturn.equals("1") ? "是" : "否";
		proInfo.put("allowReturn", allowReturn);
		String buyerRange = proInfo.get("buyerRange") == null ? "" : proInfo.get("buyerRange").toString();
		buyerRange = buyerRange.equals("1") ? "全部" : "自定义";
		proInfo.put("buyerRange", buyerRange);
		// 添加订购类型
		Integer proType = 4;
		if (proInfo.containsKey("proType")) {
			proType = (Integer) proInfo.get("proType");
		}
		proInfo.put("proTypeName", coverTypeToName(proType));

		String itemName = productPublishService.getCascadeName((String) proInfo.get("categoryCode"));
		request.setAttribute("productId", productId);// 商品编码传入页面
		request.setAttribute("pro", proInfo);
		request.setAttribute("itemName", itemName);
		request.setAttribute("auditStatus", auditStatus);
		request.setAttribute("uuid", uuid);
		request.setAttribute("auditInfo", auditInfo);
		request.setAttribute("areaName", areaName);
		return "toProductPublishInfoPage";
	}

	public String updateProInfo() {
		request = getRequest();
		String userCode = getUserCodeFromSession();
		UserInfoBean userInfo = getUserInfo(userCode);
		String corpId = userInfo.getCorpId();
		String shopId = userInfo.getShopId();
		String auditStatus = request.getParameter("auditStatus"); // 审核商品状态
		auditStatus = auditStatus == null ? "" : auditStatus;
		String uuid = request.getParameter("uuid"); // 审核商品id
		uuid = uuid == null ? "" : uuid;
		String infoFlag = request.getParameter("pageFlag");
		infoFlag = infoFlag == null ? "" : infoFlag;
		request.setAttribute("infoFlag", infoFlag);

		if (infoFlag.equals("copyProduct")) {
			String newProductId = UUID.randomUUID().toString();
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			map.put("newProductId", newProductId);
			map.put("productId", productId);
			boolean result = productPublishService.copyProduct(map);
			if (result) {
				productId = newProductId;
			}
			request.setAttribute("copyResult", result);
		}

		Map<String, Object> auditInfo = productPublishService.getAuditInfo(uuid);
		Map<String, String> map = new HashMap<String, String>();
		map.put("productId", productId);
		map.put("corpId", corpId);
		map.put("shopId", shopId);
		List<Map<String, Object>> list = productPublishService.queryTemplateInfo(map);
		Map<String, Object> proInfo = productPublishService.getProInfo(map);
		List<Map<String, Object>> areaList = productPublishService.queryShopAreaList(map);
		String areaName = "";
		if (areaList.size() > 0) {
			for (Map<String, Object> aMap : areaList) {
				areaCode += (String) aMap.get("AREA_CODE").toString() + ",";
				areaName += (String) aMap.get("ANAME").toString() + ",";
			}
			if (!"".equals(areaCode)) {
				areaCode = areaCode.substring(0, areaCode.length() - 1);
				areaName = areaName.substring(0, areaName.length() - 1);
			}
		}
		String allowReturn = proInfo.get("allowReturn") == null ? "" : proInfo.get("allowReturn").toString();
		allowReturn = allowReturn.equals("1") ? "是" : "否";
		proInfo.put("allowReturn", allowReturn);
		String buyerRange = proInfo.get("buyerRange") == null ? "" : proInfo.get("buyerRange").toString();
		buyerRange = buyerRange.equals("1") ? "全部" : "自定义";
		proInfo.put("buyerRange", buyerRange);
		String itemName = productPublishService.getCascadeName((String) proInfo.get("categoryCode"));
		System.out.println("==========proInfo==" + proInfo);

		proStatus = proStatus == null || "".equals(proStatus) ? String.valueOf((Integer) proInfo.get("PRO_STATUS"))
				: proStatus;
		String result = "toUpdateProPublishTwoPage";
		if ("1".equals(proStatus))
			result = "toUpdateProPublishThrPage";
		else if ("2".equals(proStatus))
			result = "toUpdateProPublishFourPage";

		request = getRequest();
		request.setAttribute("productId", productId);// 商品编码传入页面
		request.setAttribute("pro", proInfo);
		request.setAttribute("itemName", itemName);
		request.setAttribute("auditStatus", auditStatus);
		request.setAttribute("auditInfo", auditInfo);
		request.setAttribute("areaName", areaName);
		request.setAttribute("proName", (String) proInfo.get("proName"));
		request.setAttribute("areaCode", areaCode);
		request.setAttribute("proStatus", proStatus);
		request.setAttribute("itemCode", (String) proInfo.get("categoryCode"));
		request.setAttribute("list", list);
		return result;
	}

	/**
	 * 商品详情/更新页查询
	 */
	public void queryProductPageInfo() {
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("productId", productId);
			map.put("itemCode", itemCode);
			List<Map<String, Object>> keyList = productPublishService.getKeyWord(map);
			List<Map<String, Object>> paraList = productPublishService.getProParam(map);
			List<Map<String, Object>> proPicList = productPublishService.getProPic(map);
			List<Map<String, Object>> priceList = productPublishService.getProPrice(map);
			json.put("keyList", keyList);
			json.put("paraList", paraList);
			json.put("proPicList", proPicList);
			json.put("priceList", priceList);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductPublishAction|queryProductPageInfo：", e);
		} finally {
			str2Resp(json.toString());
		}
	}

	public String toUnfinishedProListPage() {
		return "toUnfinishedProListPage";
	}

	private String begTime;// product createTime;
	private String endTime;// product createTime;

	public void queryUnfinishedProList() {
		String userCode = getUserCodeFromSession();
		UserInfoBean userInfo = getUserInfo(userCode);
		String corpId = userInfo.getCorpId();
		String shopId = userInfo.getShopId();
		Map<String, String> map = new HashMap<String, String>();
		map.put("corpId", corpId);
		map.put("shopId", shopId);
		map.put("begTime", begTime);
		map.put("endTime", endTime);
		map.put("proName", proName);
		map.put("startIndex", getStartIndexMysql());
		map.put("rowNums", getEndIndexMysql());
		List<Map<String, Object>> list = productPublishService.queryUnfinishedProList(map);
		Long total = productPublishService.queryUnfinishedProListCount(map);
		dataGridJson = new EasyuiDataGridJson();
		dataGridJson.setRows(list);
		dataGridJson.setTotal(total);
		str2Resp(JSONObject.fromObject(dataGridJson).toString());
	}

	public void delInfo() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("productId", productId);
		String result = productPublishService.delInfo(map);
		str2Resp(result);
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getItemCode() {
		return itemCode;
	}

	public void setItemCode(String itemCode) {
		this.itemCode = itemCode;
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public String getViewCode() {
		return viewCode;
	}

	public void setViewCode(String viewCode) {
		this.viewCode = viewCode;
	}

	public String getCodeName() {
		return codeName;
	}

	public void setCodeName(String codeName) {
		this.codeName = codeName;
	}

	public String getViewFlag() {
		return viewFlag;
	}

	public void setViewFlag(String viewFlag) {
		this.viewFlag = viewFlag;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getProName() {
		return proName;
	}

	public void setProName(String proName) {
		this.proName = proName;
	}

	public String getBrandName() {
		return brandName;
	}

	public void setBrandName(String brandName) {
		this.brandName = brandName;
	}

	public String getProStatus() {
		return proStatus;
	}

	public void setProStatus(String proStatus) {
		this.proStatus = proStatus;
	}

	public String getTypePCode() {
		return typePCode;
	}

	public void setTypePCode(String typePCode) {
		this.typePCode = typePCode;
	}

	public String getBegTime() {
		return begTime;
	}

	public void setBegTime(String begTime) {
		this.begTime = begTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public String getUnitName() {
		return unitName;
	}

	public void setUnitName(String unitName) {
		this.unitName = unitName;
	}

	public String getBuyPrice() {
		return buyPrice;
	}

	public void setBuyPrice(String buyPrice) {
		this.buyPrice = buyPrice;
	}

	public String getScale() {
		return scale;
	}

	public void setScale(String scale) {
		this.scale = scale;
	}

	public String getRetailPrice() {
		return retailPrice;
	}

	public void setRetailPrice(String retailPrice) {
		this.retailPrice = retailPrice;
	}

}
